#include"Stack.h"

void StackInit(Stack* pst)
{
	assert(pst);

	pst->arr = NULL;
	pst->top = 0;
	pst->capacity = 0;
}



void StackDestroy(Stack* pst)
{
	assert(pst);

	free(pst->arr);
	pst->arr = NULL;
	pst->top = pst->capacity = 0;
}



void StackPush(Stack* pst, STDataType x)
{
	assert(pst);

	if (pst->top == pst->capacity)
	{
		pst->capacity = pst->capacity == 0 ? 4 : (pst->capacity) * 2;

		STDataType* newArr = (STDataType*)realloc(pst->arr, (pst->capacity) * sizeof(STDataType));
		if (newArr == NULL)
		{
			perror("reallocfail:");
			return;
		}

		pst->arr = newArr;
	}

		pst->arr[pst->top] = x;
		pst->top++;
}



bool StackEmpty(Stack* pst)
{
	assert(pst);

	return pst->top == 0;
}



void StackPop(Stack* pst)
{
	assert(pst);
	assert(!StackEmpty(pst));

	pst->top--;
}



STDataType StackTop(Stack* pst)
{
	assert(pst);
	assert(!StackEmpty(pst));

	return pst->arr[pst->top - 1];
}



int StackSize(Stack* pst)
{
	assert(pst);

	return pst->top;
}